package cn.zhoujing.security.demo.common.handler;

import cn.zhoujing.security.demo.common.api.CommonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 全局异常处理类
 *
 * @author zhoujing
 * @createTime 2024/2/7 - 22:34
 */
@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {


    /**
     * 认证异常处理
     *
     * @param e 异常信息
     * @return 异常结果
     */
    @ExceptionHandler(value = AuthenticationException.class)
    @ResponseBody
    public CommonResult badCredentialsExceptionHandler(AuthenticationException e) {
        log.error("[异常处理-认证异常]异常信息：", e);
        return CommonResult.failed(e.getMessage());
    }

    /**
     * 兜底异常处理
     *
     * @param e 异常信息
     * @return 异常结果
     */
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public CommonResult exceptionHandler(Exception e) {
        log.error("[异常处理-兜底异常]异常信息：", e);
        return CommonResult.failed();
    }


}
